########################################################################
# slate.py - Mechanism for generating a slate XML file from the field
#	values.  It gets its own module despite being simple because it's
#	called from multiple locations.
#
# Created 2006.06.08 by Rick Barrett
#
# Copyright (c) 2006 by Pixel Corps.  This program may be used under
# the terms of the X11 license, a copy of which should have accompanied
# this program, in the LICENSE file.
########################################################################

import util

from xml.dom.minidom import Document

import os


def Generate(Project, Scene, Element, Artist, Notes, uploader):
	"""
	Creates the slate XML file from the passed parameters

	@type Project: string
	@param Project: name of the project

	@type Scene: string
	@param Scene: name of the scene in the project

	@type Element: string
	@param Element: name of the element in the scene

	@type Artist: string
	@param Artist: name of the artist who created the element

	@type Notes: string
	@param Notes: free-form notes about the asset

	@type uploader: L{sftp.SFTPUploader}
	@param uploader: required so we can add the generated slate file to
	the list of files to upload
	"""

	# Create the XML Document	
	doc = Document()

	# Create the root element
	nodeSlate = doc.createElement("slate")
	doc.appendChild(nodeSlate)

	WriteXMLTextNode("project", Project, doc, nodeSlate)
	WriteXMLTextNode("scene", Scene, doc, nodeSlate)
	WriteXMLTextNode("element", Element, doc, nodeSlate)
	WriteXMLTextNode("artist", Artist, doc, nodeSlate)
	WriteXMLTextNode("notes", Notes, doc, nodeSlate)

	xmlFn = None
	nodeFiles = WriteXMLTextNode("files", None, doc, nodeSlate)
	for i in range(len(uploader.localFiles)):
		filename = os.path.basename(uploader.localFiles[i])
		if os.path.splitext(filename)[1].lower() == ".c4d" and not xmlFn:
			xmlFn = os.path.splitext(filename)[0] + ".xml"
		if uploader.remoteSubdirs[i]:
			filename = uploader.remoteSubdirs[i] + '/' + filename
		nodeFile = WriteXMLTextNode("file", None, doc, nodeFiles)
		nodeFile.setAttribute("name", filename)
		WriteXMLTextNode("localpath", uploader.localFiles[i], doc, nodeFile)
		WriteXMLTextNode("remotepath", uploader.remoteFiles[i], doc, nodeFile)

	if not xmlFn:
		xmlFn = os.path.splitext(uploader.localFiles[i])[0] + ".xml"

	xmlFn = os.path.join(util.GetTempPath(), xmlFn)
	xmlFile = open(xmlFn, 'w')
	xmlFile.write(doc.toprettyxml(indent = "  "))

	print '=== SLATE INFO ==='
	print 'File: ', xmlFn
	print 'Project: ', Project
	print 'Scene: ', Scene
	print 'Element: ', Element
	print 'Artist: ', Artist
	print 'Notes: ', Notes
	print '=== END SLATE ==='

	return xmlFn


def WriteXMLTextNode(name, text, doc, parent):
	"""
	Create an XML text node

	@type name: string
	@param name: the XML element name of the node

	@type text: string
	@param text: the text held within the node

	@type doc: C{xml.dom.minidom.Document}
	@param doc: the XML document to add the node to

	@type parent: XML node object
	@param parent: parent element for the new node; if None, uses parent
	"""

	if not parent: parent = doc
	node = doc.createElement(name)
	parent.appendChild(node)
	if text: node.appendChild(doc.createTextNode(text))
	return node


